import { createWebHistory, createRouter } from 'vue-router'

import Login from '../views/login.vue'
import Detail from '../views/detail.vue'
import Goods from '../views/goods.vue'
import Register from '../views/register.vue'
import Search from '../views/search.vue'
import Home from '../views/home/index.vue'
import HomeView from '../views/home/HomeView.vue'
import Classify from '../views/home/classify.vue'
import Cart from '../views/home/cart.vue'
import My from '../views/home/my.vue'
import Myorder from '../views/home/myorder.vue'
import Order from '../views/home/order.vue'


const routes = [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    { path: '/detail/:goods_id', component: Detail },
    { path: '/goods', component: Goods },
    { path: '/register', component: Register },
    { path: '/search', component: Search },
    {
        path: '/home', component: Home,
        children: [
            { path: '/home', redirect: '/home/HomeView' },
            { path: 'HomeView', component: HomeView },
            { path: 'classify', component: Classify },
            { path: 'cart', component: Cart },
            { path: 'my', component: My },
            { path: 'myorder', component: Myorder },
            { path: 'order/:id', component: Order },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    let token=localStorage.getItem('token')
    if (to.fullPath == '/home/cart' || to.fullPath == '/home/my') {
        if (!token) {
            next('/login')
        } else {
            next()
        }
    }
    else {
        next()
    }
})


export default router